package labb2.del1;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;

/**
 * Circle.
 *
 * @author Mikko Puustinen
 * @version 0.1, 2007-sep-30
 */
public class Circle extends FillableShape {
    private double diameter;

    /**
     * Creates a <code>Circle</code>.
     *
     * @param x        start position in the x-axis (pixel).
     * @param y        start position in the y-axis (pixel).
     * @param color    the Color of the <code>Circle</code>.
     * @param diameter diameter.
     */
    public Circle(double x, double y, Color color, double diameter) {
        super(x, y, color);
        this.diameter = diameter;
    }

    /**
     * Returns the diameter of the <code>Circle</code>.
     *
     * @return diameter.
     */
    public double getDiameter() {
        return diameter;
    }

    /**
     * Keeps the <code>Circle</code> inside the <code>box</code>.
     */
    public void constrain() {
        Rectangle box = getBox();
        double x0 = box.x;
        double y0 = box.y;
        double x1 = x0 + box.width - diameter;
        double y1 = y0 + box.height - diameter;
        double dx = getDX();
        double dy = getDY();

        if (getX() <= x0) dx = Math.abs(dx);
        if (getX() >= x1) dx = -Math.abs(dx);
        if (getY() <= y0) dy = Math.abs(dy);
        if (getY() >= y1) dy = -Math.abs(dy);

        setVelocity(dx, dy);
    }

    /**
     * Paints the <code>Circle</code>.
     *
     * @param g Graphics object.
     */
    public void paint(Graphics g) {
        g.setColor(getColor());
        if (isFilled())
            g.fillOval((int) getX(), (int) getY(), (int) diameter, (int) diameter);
        else
            g.drawOval((int) getX(), (int) getY(), (int) diameter, (int) diameter);
    }
}
